package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
*/

import dbPool.ConnectionDAO;
import beans.Customer;
import beans.Financial_Transaction;

public class Financial_TransactionDAO {
	
	ConnectionDAO conn = new ConnectionDAO();
    Connection con = null;
    Statement stmt = null;
    
    static {
    	try{
    		new dbPool.JDCConnectionDriver("com.mysql.jdbc.Driver","jdbc:mysql://localhost/virtualreel", "root", "root"); 
    	}catch(Exception e){
    		
    	}
    }
    
    public Connection getConnection() throws SQLException {
        return DriverManager.getConnection("jdbc:jdc:jdcpool");
    }
    
    public int createFinancial_Transaction(Financial_Transaction cBean){
    	try{
    		con = getConnection();
    		Statement stmt = con.createStatement();
    		String query = "INSERT INTO `financialtransaction`(ft_transaction_id, c_id, ft_amount_due, ft_movies_rented) VALUES('"+cBean.getFT_Transaction_Id()+"','"+cBean.getC_Id()+"','"+cBean.getFT_Amount_Due()+"','"+cBean.getFT_Movies_Rented()+"');";
    		stmt.executeUpdate(query);
    		return 0;
    	}
    	catch(Exception se){
    		Logger.getLogger(Financial_TransactionDAO.class.getName()).log(Level.SEVERE, "save failed", se);
    		return 1;
    	}
    	finally {
    		try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    }
    
    public Financial_Transaction retrieveFinancial_Transaction(Financial_Transaction cBean, String Column_Name){
    	try{
    		con = getConnection();
    		Statement stmt = con.createStatement();
    		Financial_Transaction cBeanOne = new Financial_Transaction();
    		String query = "SELECT "+Column_Name+" FROM `financialtransaction` WHERE ft_transaction_id LIKE "+cBean.getFT_Transaction_Id()+";";
    		ResultSet rs = stmt.executeQuery(query);
    		while(rs.next()){
    			cBeanOne.setFT_Transaction_Id(rs.getString("FT_Transaction_Id"));
    			cBeanOne.setC_Id(rs.getString("C_Id"));
    			cBeanOne.setFT_Amount_Due(rs.getString("FT_Amount_Due"));
    			cBeanOne.setFT_Movies_Rented(rs.getString("FT_Movies_Rented"));
    		}
    		return cBeanOne;
    	}
    	catch(Exception se){
    		Logger.getLogger(Financial_TransactionDAO.class.getName()).log(Level.SEVERE, "save failed", se);
    		return cBean;
    	}
    	finally {
    		try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    }
    
    public int updateFinancial_Transaction(Financial_Transaction cBean, String key, String value){
    	try{
    		con = getConnection();
    		Statement stmt = con.createStatement();
    		String query = "UPDATE `financialtransaction` SET  `"+key+"` =  '"+value+"' WHERE  `financialtransaction`.`ft_transaction_id` =  '"+cBean.getFT_Transaction_Id()+"';";
    		stmt.executeUpdate(query);
    		return 0;
    	}
    	catch(Exception se){
    		Logger.getLogger(Financial_TransactionDAO.class.getName()).log(Level.SEVERE, "save failed", se);
    		return 1;
    	}
    	finally {
    		try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    }
    
    public int deleteFinancial_Transaction(Financial_Transaction cBean) {
    	try{
    		con = getConnection();
    		Statement stmt = con.createStatement();
    		String query = "DELETE FROM `financialtransaction` WHERE `financialtransaction`.`ft_transaction_id` = '"+cBean.getFT_Transaction_Id()+"';";
    		stmt.executeUpdate(query);
    		return 0;
    	}
    	catch(SQLException se){
    		Logger.getLogger(Financial_TransactionDAO.class.getName()).log(Level.SEVERE, "save failed", se);
    		return 1;
    	}
    	finally {
    		try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    }

}
